﻿#region References
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ness.Engine.Entities.Primitives;
using Microsoft.Xna.Framework;
#endregion

namespace Ness.Engine.Entities
{
    /// <summary>
    /// Simple interface to allow map loading without bulky code in Engine
    /// </summary>
    class Map
    {
        #region Public Variables
        public Engine game;
        public string file;
        #endregion

        public Map(Engine game, string file)
        {
            //Set base variables
            this.game = game;
            this.file = file;

            //Check if map exists
            if (Map.Exists(file))
            {
                //Load main model
                MeshObject box = new MeshObject(game, "maps/" + file + "/" + file, Vector3.Zero);
                game.AddToScene(box);

                BoxObject boxa = new BoxObject(game, "models/box", Vector3.One, Matrix.Identity, Vector3.Zero + Vector3.Up * 15);
                game.AddToScene(boxa);

                //Load map lua file
                game.lua.DoFile("Content/maps/" + file + "/" + file + ".lua");
            }
            else
            {
                throw new Exception("Map does not exist!");
            }
        }

        /// <summary>
        /// Allows the game to check if the map exists
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool Exists(string file)
        {
            //Map directory
            if (Directory.Exists("Content/maps/" + file))
            {
                //Map model files
                if (File.Exists("Content/maps/" + file + "/" + file + ".xnb") && File.Exists("Content/maps/" + file + "/" + file + "_xna.xnb"))
                {
                    //Map lua file
                    if (File.Exists("Content/maps/" + file + "/" + file + ".lua"))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Allows the client to determine which file is missing
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string GetFile(string file)
        {
            //Map directory
            if (Directory.Exists("Content/maps/" + file))
            {
                //Map model files
                if (!File.Exists("Content/maps/" + file + "/" + file + ".xnb"))
                {
                    return "Content/maps/" + file + "/" + file + ".xnb";
                }
                if (!File.Exists("Content/maps/" + file + "/" + file + "_xna.xnb"))
                {
                    return "Content/maps/" + file + "/" + file + "_xna.xnb";
                }

                //Map lua file
                if (!File.Exists("Content/maps/" + file + "/" + file + ".lua"))
                {
                    return "Content/maps/" + file + "/" + file + ".lua";
                }
            }

            return "Content/maps/" + file;
        }
    }
}
